package com.meicet.adapter.utils

import android.view.View
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.ToastUtils
import com.meicet.adapter.BaseAdapterConfig

// 对全局的点击按键有效
fun View.onClickOnceTime(duration: Long, listener: (view: View) -> Unit) {
    // 1秒内避免连续点击
    ClickUtils.applyGlobalDebouncing(this, duration) {
        listener.invoke(it)
    }
}

// 对全局的点击按键有效
fun View.onClickOnce(listener: View.OnClickListener) {
    // 1秒内避免连续点击
    ClickUtils.applyGlobalDebouncing(this, BaseAdapterConfig.onClickTimeDebouncing) {
        listener.onClick(it)
    }
}

// 对单一的按键有效
fun View.onClickOnceSingle(listener: View.OnClickListener) {
    ClickUtils.applySingleDebouncing(this, BaseAdapterConfig.onClickTimeDebouncing) {
        listener.onClick(it)
    }
}

fun View.visibleState(): Boolean {
    return visibility == View.VISIBLE
}

fun View.visible(visible: Boolean) {
    if (visible && visibility != View.VISIBLE) {
        visibility = View.VISIBLE
    } else if (!visible && visibility != View.INVISIBLE) {
        visibility = View.INVISIBLE
    }
}

fun View.visiable(visible: Boolean) {
    if (visible && visibility != View.VISIBLE) {
        visibility = View.VISIBLE
    } else if (!visible && visibility != View.INVISIBLE) {
        visibility = View.INVISIBLE
    }
}

fun View.gone(gone: Boolean) {
    if (gone && visibility != View.GONE) {
        visibility = View.GONE
    } else if (!gone && visibility != View.VISIBLE) {
        visibility = View.VISIBLE
    }
}

fun toast(msg: String) {
    ToastUtils.showShort(msg)
}

fun toastL(msg: String) {
    ToastUtils.showLong(msg)
}
